package com.kzw.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.Student;
import com.kzw.service.StudentService;

import my.json.Jackson;
import my.mvc.Annotation.JSONOutput;
import my.mvc.RequestContext;
import my.util.ResponseUtils;
import my.util.ServletUtils;
import my.vo.Page;
import my.vo.Record;

/**
 * 一般情况：一个请求对应一个servlet 希望：在一个servlet中处理多个请求
 */
public class StudentAction {

	private StudentService studentService = new StudentService();

	/**
	 * 学生列表：AJAX
	 */
	public void list(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 15);
		Page<Student> page = new Page<>(pageNo, pageSize);
		
		String orderBy = rc.param("orderBy");
		String orderDir = rc.param("orderDir");
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		// 查询条件
		String name = rc.param("name");
		String studentId = rc.param("studentId");
		
		Record record = studentService.findPage(page, name, studentId);
		//输出JSON
		String json = Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	/**
	 * 增加或修改学生
	 * */
	public void save(RequestContext rc) throws ServletException, IOException {
		
		// 将请求参求自动包装成Bean对象 
		Student student = rc.form(Student.class);
		// id="" 自动包装成了 id=0
		if(student.getId() == 0) {
			// 新增
			studentService.save(student);
		} else {
			// 修改
			studentService.update(student);
		}
		
		// 重定向到一个查询请求
		rc.redirect(rc.contextPath() + "/action/student/list");
	}
	
	/**
	 * 删除班级
	 * */
	@JSONOutput
	public Record del(RequestContext rc) throws ServletException, IOException {
		long id = rc.param("id", 0L);
		studentService.remove(id);
		
		return new Record(true);
	}
	
	/**
	 * 获得某个学生信息
	 * */
	public void get(RequestContext rc) throws ServletException, IOException {
		String id = rc.param("id");
		if(id!=null && !id.equals("")) {
			Student student = studentService.findById(Long.parseLong(id));	
			rc.reqAttr("student", student);
			rc.forward("/pages/student_form.jsp");
		}
	}
	
	
	/**
	 * 批量删除
	 * */
	@JSONOutput
	public Record multiDel(RequestContext rc) throws ServletException, IOException {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			studentService.multiDel(ids);
		}
		
		return new Record();
	}



	/**
	 * 下载图片
	 */
	public void downImg(RequestContext rc) throws ServletException, IOException {
		long id = rc.param("id", 0L);
		Student student = studentService.findById(id);
		if (student != null) {
			String picPath = student.getPicPath();
			if (StringUtils.isNotBlank(picPath)) {
				// 进行文件下载
				String upload = rc.context().getRealPath("/upload");
				InputStream is = new FileInputStream(new File(upload, picPath));

				HttpServletResponse response = rc.response();
				OutputStream os = response.getOutputStream();
				ServletUtils.setFileDownloadHeader(response, picPath);

				// 进行流的读写，如果文件比较大时
				int len;
				byte[] b = new byte[10240];
				while ((len = is.read(b)) > 0) {
					os.write(b, 0, len);
				}
				is.close();
				os.close();
			}
		}
	}

}
